<?php
namespace api\test\controller;

use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;

Class TokenController extends RestBaseController
{

    public function createToken()
    {
        $username = '17662202329';
        //密钥
        $key = 'qwertyuiodfg';

        $JWT = new JWT();
        $arr = [
       //'签发时间'
            'iat ' => time(),
     //生效时间
            'nbf' => time() + 60,
        //过期时间
            'exp' => time() + 120 ,
            //自定义数据
            'data' =>[
                'username' => $username,
                'age' => 20
            ]
        ];
        $token = $JWT->encode($arr,$key);
        echo $token;

    }
    public function decodeToken() {
        $token = input('token');
        $jwt = new JWT();
        $key = 'qwertyuiodfg';
        $data = $jwt->decode($token,$key,['HS256']);
        halt($data);
    }
}